#include <arduino.h>

#include "resource/jzc.c"
#include "resource/img_hand.c"
#include "resource/windows_kj0.c"
#include "resource/windows_gd0.c"
#include "resource/windows_gd1.c"
#include "resource/windows_gd2.c"
#include "resource/windows_gd3.c"
#include "resource/windows_gd4.c"
#include "resource/windows_gd5.c"
#include "resource/windows_gd6.c"
#include "resource/windows_gd7.c"
#include "resource/windows_gd8.c"
#include "resource/windows_gd9.c"
#include "resource/windows_gd10.c"
#include "resource/windows_gd11.c"

LV_IMG_DECLARE(jzc);
LV_IMG_DECLARE(windows_kj0);
LV_IMG_DECLARE(windows_gd0);
LV_IMG_DECLARE(windows_gd1);
LV_IMG_DECLARE(windows_gd2);
LV_IMG_DECLARE(windows_gd3);
LV_IMG_DECLARE(windows_gd4);
LV_IMG_DECLARE(windows_gd5);
LV_IMG_DECLARE(windows_gd6);
LV_IMG_DECLARE(windows_gd7);
LV_IMG_DECLARE(windows_gd8);
LV_IMG_DECLARE(windows_gd9);
LV_IMG_DECLARE(windows_gd10);
LV_IMG_DECLARE(windows_gd11);

static void anim_x_cb(void * var, int32_t v)
{
    lv_obj_set_x((_lv_obj_t*)var, v);
}

//================================================================
//================================================================
static void Setup1()
{
    lv_obj_t *docker = page_group[PAGE_Main].cont;
    lv_obj_move_foreground(docker);
    lv_obj_align(docker, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t *img1 = lv_img_create(docker);
    lv_img_set_src(img1, &jzc);
    lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t *label1 = lv_label_create(docker);
    lv_label_set_text(label1, "Hello JKY!");
    lv_obj_align(label1, LV_ALIGN_CENTER, 0, 0);
}

static void Exit1()
{
    lv_obj_t *docker = page_group[PAGE_Main].cont;
    lv_anim_t a;
    lv_anim_init(&a);                                  //初始化动画对象
    lv_anim_set_var(&a, docker);                       //为对象设置一个动画
    lv_anim_set_values(&a, lv_obj_get_x(docker), 240); //设置一个动画的开始和结束值
    lv_anim_set_time(&a, 500);                         //设置一个动画时间
    lv_anim_set_exec_cb(&a, anim_x_cb);                //设置一个动画回调
    lv_anim_set_path_cb(&a, lv_anim_path_overshoot);   //设置一个动画的路径
    lv_anim_start(&a);                                 //开始动画
    //----------------------------------------------------------------
    uint32_t lastTime = millis();
    while(millis() - lastTime <= 800)
    {
        lv_task_handler();
    }
    lv_obj_clean(docker);
}

static void Event1(int key, int event)
{
   if (key == KEY_BUTTON_L) {
      manager.PagePush(PAGE_Watch);
   }
   if (key == KEY_BUTTON_R) {
      manager.PagePush(PAGE_Window);
   }
}

//================================================================
//================================================================
static lv_obj_t * meter;
static void set_value(void * indic, int32_t v)
{
    lv_meter_set_indicator_end_value(meter, (lv_meter_indicator_t*)indic, v);
}
static void Setup2()
{
    lv_obj_t *docker = page_group[PAGE_Watch].cont;
    lv_obj_move_foreground(docker);
    meter = lv_meter_create(docker);
    lv_obj_set_size(meter, 240, 240);
    lv_obj_center(meter);

    /*Create a scale for the minutes*/
    /*61 ticks in a 360 degrees range (the last and the first line overlaps)*/
    lv_meter_scale_t * scale_min = lv_meter_add_scale(meter);
    lv_meter_set_scale_ticks(meter, scale_min, 61, 1, 10, lv_palette_main(LV_PALETTE_GREY));
    lv_meter_set_scale_range(meter, scale_min, 0, 60, 360, 270);

    /*Create an other scale for the hours. It's only visual and contains only major ticks*/
    lv_meter_scale_t * scale_hour = lv_meter_add_scale(meter);
    lv_meter_set_scale_ticks(meter, scale_hour, 12, 0, 0, lv_palette_main(LV_PALETTE_GREY));               /*12 ticks*/
    lv_meter_set_scale_major_ticks(meter, scale_hour, 1, 2, 20, lv_color_black(), 10);    /*Every tick is major*/
    lv_meter_set_scale_range(meter, scale_hour, 1, 12, 330, 300);       /*[1..12] values in an almost full circle*/

    LV_IMG_DECLARE(img_hand)

    /*Add a the hands from images*/
    lv_meter_indicator_t * indic_min = lv_meter_add_needle_img(meter, scale_min, &img_hand, 5, 5);
    lv_meter_indicator_t * indic_hour = lv_meter_add_needle_img(meter, scale_min, &img_hand, 5, 5);

    /*Create an animation to set the value*/
    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_exec_cb(&a, set_value);
    lv_anim_set_values(&a, 0, 60);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_time(&a, 2000);     /*2 sec for 1 turn of the minute hand (1 hour)*/
    lv_anim_set_var(&a, indic_min);
    lv_anim_start(&a);

    lv_anim_set_var(&a, indic_hour);
    lv_anim_set_time(&a, 24000);    /*24 sec for 1 turn of the hour hand*/
    lv_anim_set_values(&a, 0, 60);
    lv_anim_start(&a);
}

static void Exit2()
{
    lv_obj_t *docker = page_group[PAGE_Watch].cont;
    lv_anim_del_all();
    lv_obj_clean(docker);
}

static void Event2(int key, int event)
{
   if (key == KEY_BUTTON_M) manager.PagePop();
}

//================================================================
//================================================================
static const lv_img_dsc_t * anim_imgs[12] = {
    &windows_gd0,
    &windows_gd1,
    &windows_gd2,
    &windows_gd3,
    &windows_gd4,
    &windows_gd5,
    &windows_gd6,
    &windows_gd7,
    &windows_gd8,
    &windows_gd9,
    &windows_gd10,
    &windows_gd11,
};

static void Setup3()
{
    lv_obj_t *docker = page_group[PAGE_Window].cont;
    lv_obj_move_foreground(docker);
    lv_obj_t *img1 = lv_img_create(docker);
    lv_img_set_src(img1, &windows_kj0);
    lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
    lv_obj_t * animimg0 = lv_animimg_create(docker);
    //lv_obj_center(animimg0);
    lv_obj_align(animimg0, LV_ALIGN_CENTER, 5, 55);
    lv_animimg_set_src(animimg0, (lv_img_dsc_t **) anim_imgs, 12);
    lv_animimg_set_duration(animimg0, 1000);
    lv_animimg_set_repeat_count(animimg0, LV_ANIM_REPEAT_INFINITE);
    lv_animimg_start(animimg0);
}

static void Exit3()
{
    lv_obj_t *docker = page_group[PAGE_Window].cont;
    lv_anim_del_all();
    lv_obj_clean(docker);
}